package com.huilian.iotbox.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.dao.CommodityDao;
import com.huilian.iotbox.admin.service.CommodityService;
import com.huilian.iotbox.data.dto.CommodityDto;
import com.huilian.iotbox.data.po.Commodity;
import com.huilian.iotbox.data.vo.CommodityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityDao commodityDao;

    @Override
    public PageInfo list(CommodityDto commodityDto) {
        PageHelper.startPage(commodityDto.getPageNum(),commodityDto.getPageSize()," c.createTime desc");
        List<CommodityVo> list = commodityDao.list(commodityDto);
        PageInfo pageInfo = new PageInfo(list);
        if (list.size() < 10) {
            pageInfo.setTotal(commodityDto.getPageSize() * commodityDto.getPageNum());
        } else {
            pageInfo.setTotal(commodityDto.getPageSize() * commodityDto.getPageNum() + 1);
        }
        return pageInfo;
    }

    @Override
    public int insert(Commodity commodity) {
        return commodityDao.insert(commodity);
    }
}
